<!--
 * 流程定义
 * @author luguoxiang
 * @date 2025-08-02
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
-->
<template>
  <edit-container ref="containerRef" v-bind="containerConfig">
    <designer :data="data" />
  </edit-container>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { EditContainer, useEditContainer } from '@/components';
import Designer from '@/components/flow/Designer.vue';
import api, { subject } from './api';
import type { SoarProcess } from './schema';
import { Node } from '@/soar/components/LgFlowChat/type';

const emits = defineEmits(['ok']);

const NODES = [
  {
    type: 'start',
    key: 'apply',
    name: '发起人',
    description: '发起人节点',
    fieldPermits: [],
    buttons: [{ key: 'submit', name: '提交' }],
    nodeListeners: [],
    processListeners: [],
  },
  {
    type: 'end',
    key: 'end',
    name: '流程结束',
    description: '',
    processListeners: [],
  },
] as Node[];

const containerRef = ref();
const formRef = ref();
const { containerConfig, formConfig, loading, open, data } = useEditContainer<SoarProcess>({
  header: subject,
  containerRef,
  formRef,
  data: {
    disabled: false,
    nodes: NODES,
  },
  getData: (params: any) => {
    return api.detail(params.id).then((res: any) => {
      if (!res.nodes || res.nodes.length < 2) {
        res.nodes = NODES;
      }
      return res;
    });
  },
  onOk: async ({ values, data, params, isUpdate, changed }) => {
    await api.saveOrUpdate(data, isUpdate);
    emits('ok', values);
  },
  containerProps: {
    width: 'var(--app-main-width)',
  },
});

defineExpose({ open });
</script>
